package com.srlg.liteflow.controller;

import com.srlg.liteflow.ctx.CustomContext;
import com.yomahub.liteflow.builder.el.ELBus;
import com.yomahub.liteflow.builder.el.LiteFlowChainELBuilder;
import com.yomahub.liteflow.builder.el.ThenELWrapper;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * lite flow  控制器
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("lite")
public class LiteFlowController {

    final FlowExecutor flowExecutor;

    @GetMapping("abc")
    public String execABCFlow() {
        LiteflowResponse res = flowExecutor.execute2Resp("chain");
        log.info("========> 结果:{}", res.isSuccess());

        return res.getMessage();
    }

    @GetMapping("switch")
    public String execSwitchFlow() {
        LiteflowResponse res = flowExecutor.execute2Resp("switch");

        return res.getMessage();
    }

    @GetMapping("switchId")
    public String execSwitchIdFlow() {
        LiteflowResponse res = flowExecutor.execute2Resp("switchId");

        return res.getMessage();
    }

    @GetMapping("switchTag")
    public String execSwitchTagFlow() {
        LiteflowResponse res = flowExecutor.execute2Resp("switchTag");

        return res.getMessage();
    }

    @GetMapping("switchSub")
    public String execSwitchSubFlow() {
        LiteflowResponse res = flowExecutor.execute2Resp("switchSub");

        return res.getMessage();
    }

    @GetMapping("bool")
    public String execBoolFlow() {
        LiteflowResponse res = flowExecutor.execute2Resp("bool");

        return res.getMessage();
    }

    @GetMapping("for")
    public String execForFlow() {
        LiteflowResponse res = flowExecutor.execute2Resp("for");

        return res.getMessage();
    }

    @GetMapping("extend")
    public String execExtendFlow() {
        LiteflowResponse res = flowExecutor.execute2Resp("impA");

        return res.getMessage();
    }

    @GetMapping("ctx")
    public String execCtxFlow() {

        LiteflowResponse res = flowExecutor.execute2Resp("chain", CustomContext.class);

        return res.getMessage();

    }

    @GetMapping("ctxMethod")
    public String execCtxMethod(){

        CustomContext context = new CustomContext();
        context.setName("srlg");
        context.setAge(12);

        LiteflowResponse res = flowExecutor.execute2Resp("execMa", null, context);

        return res.getMessage();
    }

    @GetMapping("jChain")
    public String execJavaChain(){

        ThenELWrapper then = ELBus.then(
                "ja"
//                ELBus.when("jb", ELBus.then("jc", "jd"))
        );

        LiteFlowChainELBuilder.createChain().setChainName("firstChain").setEL(
                // 输出el表达式
                then.toEL()
        ).build();

        LiteflowResponse res = flowExecutor.execute2Resp("firstChain", null, CustomContext.class);

        return res.getMessage();
    }
}
